//
//  ViewController.m
//  IOS-9rule
//
//  Created by RIOS on 2018/4/3.
//  Copyright © 2018年 RIOS. All rights reserved.
//

#import "ViewController.h"
#import "CZapp.h"
#import "CZappView.h"

@interface ViewController ()
@property (nonatomic,strong) NSArray *array;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    [self addView];
    [self addViewForXib];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSArray *)array{
   
    if (_array == nil) {
        NSString *path  =[[NSBundle mainBundle] pathForResource:@"download.plist" ofType:nil];
        
         NSArray *arrayApp = [NSArray arrayWithContentsOfFile:path];
        
        
        NSMutableArray *marray =  [NSMutableArray new];
        
        for (NSDictionary *dict in arrayApp) {
            CZapp *model = [CZapp appWithDict:dict];
            [marray addObject:model];
        }

        NSLog(@"%@",marray);
        _array = marray;
    }
    return  _array;
    
}

- (void) addView{
    CGFloat width = self.view.frame.size.width;
    CGFloat appw = 75;
    CGFloat apph = 90;
    CGFloat marginTop = 30;
    CGFloat marginLetf = (width-appw*3)/4;
    
    CGFloat imageLelf = 15;
    CGFloat labelH = 20;
    
    for (int i =0; i<9; i++) {
        CZapp *model = self.array[i];
        UIView *vView = [UIView new];
        vView.frame =CGRectMake(marginLetf+(marginLetf+appw)*(i%3),
                                marginTop+(marginTop+apph)*(i/3), appw, apph);
        
        [self.view addSubview:vView];
        
        UIImageView *vImage = [UIImageView new];
        vImage.frame = CGRectMake(imageLelf, 0, appw-imageLelf*2, appw-imageLelf*2);
        [vImage setImage:[UIImage imageNamed:model.icon]];
        [vView addSubview:vImage];
        
        
        UILabel *label = [UILabel new];
        label.frame = CGRectMake(0, appw-imageLelf*2, appw, labelH);
        label.font = [UIFont systemFontOfSize:11];
        label.text =model.label;
        [label setTextAlignment:NSTextAlignmentCenter];
        [vView addSubview:label];
        
        UIButton *button = [UIButton new];
        button.frame = CGRectMake(0, appw-imageLelf*2+labelH, appw, apph-(appw-imageLelf*2)-labelH);
        [button setTitle:@"下载" forState:UIControlStateNormal] ;
        [button setTitle:@"已下载" forState:UIControlStateDisabled] ;
        button.titleLabel.font  = [UIFont systemFontOfSize:13];
        [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
        button.tag = i;
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [vView addSubview:button];
        
    }

}

- (void) addViewForXib{
    CGFloat width = self.view.frame.size.width;
    CGFloat appw = 75;
    CGFloat apph = 90;
    CGFloat marginTop = 30;
    CGFloat marginLetf = (width-appw*3)/4;
 
    
    for (int i =0; i<9; i++) {
        CZappView *vView = [CZappView appView];
        vView.frame =CGRectMake(marginLetf+(marginLetf+appw)*(i%3),
                                marginTop+(marginTop+apph)*(i/3), appw, apph);
        [self.view addSubview:vView];
        
        CZapp *model = self.array[i];
        model.positon = i;
        [vView setModel:model];
        
    }
    
}

- (void) btnClick:(UIButton *)btn{
    NSLog(@"点击了:%ld",(long)btn.tag);
}














@end
